<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ajax XHR</title>
    <script src="./ajax.js"></script>
</head>
<body>
    <form id="form" action="/login" >
        <input type="text" name="name" id=""><br />
        <input type="text" name="pwd" id=""><br />
        <button type="submit">提交</button>
    </form>
    <script>

        /*
            Promise:
                status 状态: Padding, Fulfilled, Rejected

                实例方法
                .then   
                .catch
                .finally
                
                静态方法

        */

        window.onload = function() {

            let getOrderList = function() {
                return new Promise(function(resolve, reject) {
                    ajax({
                        url: '/getOrderList',
                        method: 'POST',
                        dataType: 'json',
                        success: function(data) {
                            console.log('getOrderList', data);
                            resolve(data);
                        }, 
                        error: function(err) {
                            console.error('getOrderList', data);
                            reject(err);
                        }
                    })
                })
            }

            let doLogin = new Promise(function(resolve, reject) {
                
                // reject()     Rejected
                // resolve():   Fulfilled
                ajax({
                    url: '/login',
                    method: 'POST',
                    dataType: 'json',
                    success: function(data) {
                        console.log('login', data);
                        resolve(data);
                    }, 
                    error: function(err) {
                        console.error('login', data);
                        reject(err);
                    }
                })
            })
            .then(function(resolveData) {
                return '111'
            }, function(rejectData) {
                
            })
            .then((n) => {
                console.log(n)
                throw new Error('test catch')
                return '222'
            })
            .then((n) => {
                console.log(n)
                return 333
            })
            .catch((err) => {
                console.log(err);
                return 444
            })
            .finally(() => {
                console.log('汇总')
            })

        }
        // 
        // /getOrderList
        // /getBalance


    </script>
</body>
</html>